package com.toolset.Activity.UserItemActivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.toolset.Activity.BaseActivity;
import com.toolset.Activity.LoginActivity;
import com.toolset.Bean.User;
import com.toolset.Controller.ActivityController;
import com.toolset.R;

import org.litepal.crud.DataSupport;


/**
 * 用户修改密码 活动
 */

public class ModifyPassword extends BaseActivity {

    private EditText oldPassword;
    private EditText newPassword;
    private EditText confirmPassword;

    private Button modifyPasswordBtn;

    Intent intent;

    User user;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_password);

        //初始化事件
        initEven();

        //获取 父视图发来的数据
        getParentData();

        //设置事件中的数据
        setEventData();

        //监听修改密码按钮
        modifyPasswordBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkDate()){

                    //更新密码
                    user.setUserPassword(newPassword.getText().toString());

                    //修改数据库密码
                    user.update(user.getId());

                    Toast.makeText(ModifyPassword.this,"密码修改成功！",Toast.LENGTH_SHORT).show();

                    //清空所有活动 重新登陆
                    ActivityController.finishAll();

                    //跳转到登陆页
                    Intent toLoinIntent = new Intent(ModifyPassword.this, LoginActivity.class);

                    startActivity(toLoinIntent);

                }
            }
        });

    }



    //初始化事件
    private void initEven() {
        oldPassword = (EditText) findViewById(R.id.modify_Password_oldUserPassword);
        newPassword = (EditText) findViewById(R.id.modify_Password_newUserPassword);
        confirmPassword = (EditText) findViewById(R.id.modify_Password_confirmUserPassword);
        modifyPasswordBtn = (Button) findViewById(R.id.modify_PasswordBtn);
    }

    //获取 父视图发来的数据
    public void getParentData() {
        intent = getIntent();
        user = (User) intent.getSerializableExtra("user");
    }

    //设置事件中的数据
    private void setEventData() {
        oldPassword.setText(user.getUserPassword());
    }

    //检查数据
    private boolean checkDate(){

        String newPass = newPassword.getText().toString();
        String confirmPass =  confirmPassword.getText().toString();

        if (newPass.length()==0){
            Toast.makeText(ModifyPassword.this,"密码不能为空！",Toast.LENGTH_SHORT).show();
            return false;
        }
        if (confirmPass.length()==0){
            Toast.makeText(ModifyPassword.this,"确认密码不能为空！",Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!newPass.equals(confirmPass)){
            Toast.makeText(ModifyPassword.this,"两次密码不一致！",Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }
}
